// index.js
// 获取应用实例
import {request} from "../../utils/request"

const app = getApp()

Page({
  data: {
    show: false,
    show2: false,
    show3: false,
    show4: false,
    text: '请选择线路电压',
    text2: '请选择站名称',
    text3: '请选择线路',
    text4: '请选择设备',
    list1: [],
    list2: [],
    list3: [],
    list4: [],
    voltageLevel: '',
    stationName: '',
    lat: '',
    lon: '',
    name: ''
  },

  onLoad() {
    console.log(app.globalData.token)
  },
  handleDY() {
    request({
      url: "/navigate/listVoltage",
      method: 'GET',
      header: {
        'Authorization': app.globalData.token
      }
    })
    .then(res => {
      if(res.data.code === 500) {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
        return
      }
      this.setData({
        list1: res.data.data,
        show: !this.data.show,
        show2: false,
        show3: false,
        show4: false
      })
    })

  },
  bindShowMsg2() {
    this.setData({
      show2: !this.data.show2,
      text3: '请选择出线',
      lineList3: [],
      show3: false,
      show4: false
    })
  },
  bindShowMsg3() {
    this.setData({
      show3: !this.data.show3,
      show4: false
    })
  },
  bindShowMsg4() {
    this.setData({
      show4: !this.data.show4
    })
  },

  selectItem(e) {
    var voltageLevel = e.currentTarget.dataset.name
    this.setData({
      voltageLevel
    })
    if (voltageLevel.length == 0 || voltageLevel == undefined) {
      return false;
    } else {
      request({
        url: "/navigate/listStation?voltageLevel=" + voltageLevel,
        method: 'GET',
        header: {
          'Authorization': app.globalData.token
        }
      })
      .then(res => {
        this.setData({
          list2: res.data.data
        })
      })
      this.setData({
        text: voltageLevel,
        show: false,
        text2: '请选择站名称',
        text3: '请选择线路',
        text4: '请选择设备', 
      })
    }  
  },
  selectItem2(e) {
    var stationObj = e.currentTarget.dataset.name
    this.setData({
      text2: stationObj.stationName
    })
    if (stationObj == {} || stationObj == undefined) {
      return false;
    } else {
      request({
        url: "/navigate/listLine/" + stationObj.id,
        method: 'GET',
        header: {
          'Authorization': app.globalData.token
        },
      })
      .then(res => {
        this.setData({
          list3: res.data.data,
          show2: false,
          text3: '请选择线路',
          text4: '请选择设备',
        })
      })
    }
  },
  selectItem3(e) {
    var lineObj = e.currentTarget.dataset.name
    this.setData({
    })
    if (lineObj == {} || lineObj == undefined) {
      return false;
    } else {
      request({
        url: "/navigate/listCoordinate/" + lineObj.id,
        method: 'GET',
        header: {
          'Authorization': app.globalData.token
        },
        data: {

        }
      })
      .then(res => {
        this.setData({
          list4: res.data.data,
          show3: false,
          text3: lineObj.lineName,
          text4: '请选择设备',
        })
      console.log(this.data.list4)
      })
    }
  },
  selectItem4(e) {
    var deviceObj = e.currentTarget.dataset.name
    console.log(deviceObj)
    const lat = deviceObj.lat
    const lon = deviceObj.lon
    const name = deviceObj.deviceName
    this.setData({
      lat,
      lon,
      name
    })
    if (deviceObj == {} || deviceObj == undefined) {
      return false;
    } else {
      this.setData({
        show4: false,
        text4: deviceObj.deviceName
      })
    }
  },
  getName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  getInput(e) {
    console.log(e)
    this.setData({
      reason: e.detail.value
    })
  },
  handleShowLine() {
    if(this.data.text.indexOf('请选择') &&  this.data.text2.indexOf('请选择') && this.data.text3.indexOf('请选择') && this.data.text4.indexOf('请选择')) {
      wx.navigateTo({
        url: '/pages/location/location?lat=' + this.data.lat + '&lon=' + this.data.lon + '&name=' + this.data.name,
      })
    } else {
      wx.showToast({
        title: '请将需要选择的内容，填写完整',
        icon: 'none',
        mask: true
      })
    }
  },
  handleAddLine() {
    wx.navigateTo({
      url: '/pages/add/add',
    })
  }
})
